<!--
 * @Descripttion:
 * @Copyright: 武汉一一零七科技有限公司©版权所有
 * @Link: www.wo-shop.net
 * @Contact: QQ:2487937004
 * @LastEditors: cbing
 * @LastEditTime: 2020-08-01 06:54:10
-->
<template>
	 <view class="third-login-wrap">
		 <!-- #ifdef APP-PLUS -->
		 <view class="third-login-title" v-if="isWechatShow">{{$t('快捷登录')}}</view>
		 <view class="third-login u-flex u-row-center" v-if="isWechatShow">
		 	<u-icon name="weixin-fill" color="#09D068" size="58" @click="wxLogin"></u-icon>
		 </view>
		 <!-- #endif -->
	 </view>
</template>

<script>
	export default {
		name: "thirdLogin",
		data() {
			return {
				isWechatShow: false
			}
		},
		props: {
		},
		created() {
			this.getThirdLogin()
		},
		onLoad() {
			// #ifdef APP-PLUS
			this.getCid();
			// #endif
		},
		methods: {
			getCid() {
				//从缓存中取cid值，如果取到就不调取接口获取
				var info = plus.push.getClientInfo();
				var cid = info.clientid;
				var token = info.token;
				if (cid && token) {
					uni.setStorageSync("devicetoken", cid);
					this.devicetoken = uni.getStorageSync("devicetoken");
				}
			},
			// 判断是否开启第三方登录
			getThirdLogin() {
				this.$http.getThirdLogin().then((res) => {
					if (res.status == 200) {
						this.isWechatShow = res.data.open
					}
				})
			},
			// 第三方注册 微信
			wxLogin() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						console.log('res:wxLogin ', res);
						uni.getUserInfo({
							provider: 'weixin',
							success: (infoRes) => {
								this.$loading()
								this.$http.thirdLogin({
									oauth: 'weixin_app',
									openid: infoRes.userInfo.openId,
									nick_name: infoRes.userInfo.nickName,
									iconurl: infoRes.userInfo.avatarUrl,
									uniongender: infoRes.userInfo.gender,
									unionid: infoRes.userInfo.unionId,
									devicetoken: uni.getStorageSync("devicetoken")
								}).then(result => {
									if (result.status == 200) {
										this.$hideLoading()
										this.$toast(result.mess)
										uni.setStorageSync('token', result.data.token);
										uni.setStorageSync('identity', result.data
											.identity);
										uni.setStorageSync('role', result.data
											.role); //角色身份
										uni.setStorageSync('role_id', result.data.shop_id);
										uni.setStorageSync('pid', result.data.shop_id);
										uni.setStorageSync('serviceShopId', result.data
											.serviceShopId);
										this.$store.commit('loginStatus', true)
										uni.$emit('accountLogin', {
											isLogin: true
										})
										
										uni.closeSocket({
											success() {
												console.log('socket关闭成功');
											},
											fail(res) {
												console.log(res);
											}
										})
										
										setTimeout(() => {
											this.im()
											uni.switchTab({
												url: '/pages/tabBar/home'
											})
										}, 1500);
									}
									
									if (result.status == 400 || result.data.info == 'unregister') {
										console.log(result)
										this.$u.route({
											url:'pagesB/login/bindPhone', 
											params: {
												oauth: 'weixin_app',
												openid: infoRes.userInfo.openId,
												nick_name: infoRes.userInfo.nickName,
												iconurl: infoRes.userInfo.avatarUrl,
												uniongender: infoRes.userInfo.gender,
												unionid: infoRes.userInfo.unionId,
												devicetoken: uni.getStorageSync("devicetoken")
											}
										});
									}
			
								}).catch(err => {
									console.log(JSON.stringify(err));
			
								})
			
							}
						});
			
					},
					fail: (err) => {
						console.log('login fail:', err);
					}
				});
			},
			
		}

	}
</script>
<style lang="scss" scoped>
 .third-login-wrap{
	 position: fixed;
	 bottom: 100rpx;
	 width: 100%;
	 .third-login-title{
		 text-align: center;
		 font-size: 24rpx;
		 color: #d0d0d0;
		 margin-bottom: 10rpx;
	 }
 }
</style>